import {
  whether_pay_password,
  bankcardList,
  putBtn,
  bill,
  deBannk,
  withdrawPrivate
} from '../../../../api.js'
var app = getApp(); //获取App内部的信息
Page({

  /**
   * 页面的初始数据
   */
  data: {

    Length: 6,        //输入框个数  
    isFocus: true,    //聚焦  
    Value: "",        //输入的内容  
    ispassword: true, //是否密文显示 true为密文， false为明文。
    btn_none: false,
    allmany: '',
    many_inp: '',
    bank_alert: false,
    allmany_num: '',
    bank_alert_height: '',
    focus: false,
    bankList: [],
    bank_lis_num: 0,
    radiotype:1,
    vip_level:0
  },
  colse(){
    this.setData({
      bank_alert:true
    })
  },
  Focus(e) {
    var that = this;
    console.log(e.detail.value);
    var inputValue = e.detail.value;
    that.setData({
      Value: inputValue,
    })
  },
  Tap() {
    var that = this;
    that.setData({
      isFocus: true,
    })
  },
  formSubmit(e) {
    this.setData({
      showpay:false
    })
    console.log(e.detail.value.password);
    putBtn({
        token: app.globalData.token,
        money: this.data.allmany_num,
      pay_password: e.detail.value.password
    }).then(res=>{
      console.log(res)
      if (res.data.code == 666) {
            wx.showToast({
              title: res.data.msg,
            })
            setTimeout(function() {
              wx.redirectTo({
                url: '/pages/user/bill/bill'
              })
            }, 1500)
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        })
  },  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    bill({
      token: app.globalData.token
    }).then(res => {
      if (res.data.code == 666) {
        that.setData({
          allmany: res.data.data.accountBalance,
        })
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
      if (that.data.radiotype == 2) {
        wx.showLoading({
          title: '正在提交申请',
        })
        withdrawPrivate({
          token: app.globalData.token,
          money: that.data.allmany_num,
          pay_password: e.detail.value,
          bankcard_id: that.data.bankList[that.data.bank_lis_num].bank_id
        }).then(res =>{
          if (res.data.code == 666) {
            wx.hideLoading()
            wx.showToast({
              title: res.data.msg,
            })
            setTimeout(function () {
              wx.redirectTo({
                url: '/pages/user/bill/bill'
              })
            }, 1500)
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    bankcardList({
      token: app.globalData.token
    }).then(res => {
      if (res.data.code == 666) {
        that.setData({
          bankList: res.data.data.list,
          vip_level: res.data.data.data.vip_level
        })
        console.log(that.data.bankList)
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function() {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
      
    })
  },
  up_inp: function(e) {
    var num = e.detail.value;
    var that = this;
    console.log(that.data.allmany + '-' + num)
    if (num == '') {
      that.setData({
        allmany_num: 0,
        btn_none: false
      })
    } else if (num == 0) {
      that.setData({
        btn_none: false
      })
    } else {
      that.setData({
        btn_none: true,
        allmany_num: num
      })
    }
  },
  allt: function() {
    var that = this;
    var num = that.data.allmany;
    if (num == 0) {
      return false;
    }
    that.setData({
      many_inp: num,
      btn_none: true,
      allmany_num: num
    })
  },
  ti_alert: function() {
    var that = this
    whether_pay_password({
      token: app.globalData.token
    }).then(res => {
      console.log(res.data.code)
      if (res.data.code == 666) {
        that.setData({
          bank_alert: true,
          focus_input: true,
          showpay:true
        })
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function() {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.navigateTo({
          url: '/pages/user/bill/putForward/newPassword/newPassword',
        })
      }
    })
  },
  // bindfocus_inp: function(e) {
  //   var num = e.detail.value;
  //   this.setData({
  //     bank_alert_height: num
  //   })
  // },
  bank_alert_close: function() {
    this.setData({
      bank_alert: false
    })
  },
  // input_foc: function() {
  //   this.setData({
  //     focus_input: true
  //   })
  // },
  // bank_input_bind: function(e) {
  //   var num = e.detail.value;
  //   var that = this
  //   that.setData({
  //     bank_input_bind_num: num.length
  //   })
  //   if (num.length > 5) {
  //     that.setData({
  //       bank_alert: false,
  //       focus_input: false,
  //       bank_password: '',
  //       bank_input_bind_num: 0
  //     })
  //     if (that.data.radiotype == 1){
  //       putBtn({
  //         token: app.globalData.token,
  //         money: that.data.allmany_num,
  //         pay_password: e.detail.value
  //       }).then(res => {
  //         console.log(res)
  //         if (res.data.code == 666) {
  //           wx.showToast({
  //             title: res.data.msg,
  //           })
  //           setTimeout(function() {
  //             wx.redirectTo({
  //               url: '/pages/user/bill/bill'
  //             })
  //           }, 1500)
  //         } else {
  //           wx.showToast({
  //             title: res.data.msg,
  //             icon: 'none'
  //           })
  //         }
  //       })
  //     }
  //     if (that.data.radiotype == 2){
  //       wx.showLoading({
  //         title: '正在提交申请',
  //       })
  //       withdrawPrivate({
  //         token: app.globalData.token,
  //         money: that.data.allmany_num,
  //         pay_password: e.detail.value,
  //         bankcard_id: that.data.bankList[that.data.bank_lis_num].bank_id
  //       }).then(res =>{
  //         if (res.data.code == 666) {
  //           wx.hideLoading()
  //           wx.showToast({
  //             title: res.data.msg,
  //           })
  //           setTimeout(function () {
  //             wx.redirectTo({
  //               url: '/pages/user/bill/bill'
  //             })
  //           }, 1500)
  //         } else {
  //           wx.showToast({
  //             title: res.data.msg,
  //             icon: 'none'
  //           })
  //         }
  //       })
  //     }
  //   }
  // },
  bank_inpu: function() {
    this.setData({
      focus: true
    })
  },
  choice(e) {
    var num = e.currentTarget.dataset.num
    var that = this
    if (that.data.bank_lis_num == num) {
      return
    } else {
      that.setData({
        bank_lis_num: num
      })
    }
  },
  bank_set(e){
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/banklist/bannkSet/bannkSet?id=' + id,
    })
  },
  bank_new(){
    wx.navigateTo({
      url: '/pages/banklist/bannkSet/bannkSet',
    })
  },
  bank_del(e){
    var that = this
    var id = e.currentTarget.dataset.id
    var index = e.currentTarget.dataset.index
    wx.showModal({
      title: '提示',
      content: '是否删除？',
      confirmColor:'#F7712E',
      success(res) {
        if (res.confirm) {
          deBannk({
            token: app.globalData.token,
            bank_id: id
          }).then(res => {
            if (res.data.code == 666) {
              that.data.bankList.splice(index, 1)
              that.setData({
                bankList: that.data.bankList
              })
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none'
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })    
  },
  radioChange(e){
    console.log(e.detail.value)
    this.setData({
      radiotype: e.detail.value
    })
  }
})